package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.FaultHourCollectDto;
import com.enjoyor.soa.traffic.core.efoms.domain.FaultHourCollect;
import com.enjoyor.soa.traffic.core.efoms.service.IFaultHourCollectService;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ManageFaultHourCollectService {

    @Autowired
    private IFaultHourCollectService faultHourCollectService;

    /**
     * 查询设备异常数据
     * @param faulHourId 异常分时编号
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @param collectStartTime 统计开始时间
     * @param collectEndTime 统计结束时间
     * @return
     */
	public ResultPojo selectFaultHourCollectList(String devCategoryCode, String devTypeCode,
			String collectStartTime, String collectEndTime) {
        try {
			List<FaultHourCollect> faultHourCollectList = faultHourCollectService.selectFaultHourCollectList(
					devCategoryCode, devTypeCode, collectStartTime, collectEndTime);
			List<FaultHourCollectDto> faultHourCollectDtoList = ReflectHelper.listToList(faultHourCollectList,
					FaultHourCollectDto.class);
			return ResultHelper.getResult(faultHourCollectDtoList);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
    
	/**
     * 统计当前时间实时异常数
     * @param devCategoryCode 设备种类编号
     * @param devTypeCode 设备类型编号
     * @return
     */
	public ResultPojo selectFaultRealCollectList(String devCategoryCode, String devTypeCode) {
        try {
			List<FaultHourCollect> faultHourCollectList = faultHourCollectService.selectFaultRealCollectList(
					devCategoryCode, devTypeCode);
			List<FaultHourCollectDto> faultHourCollectDtoList = ReflectHelper.listToList(faultHourCollectList,
					FaultHourCollectDto.class);
			return ResultHelper.getResult(faultHourCollectDtoList);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
	
    /**
     * 新增设备异常数据
     * @param faultHourCollectDto
     * @param username
     */
    public ResultPojo insertFaultHourCollect(FaultHourCollectDto faultHourCollectDto, String username) {
        try {
            if (faultHourCollectDto == null)
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            FaultHourCollect faultHourCollect = ReflectHelper.beanToBean(faultHourCollectDto, FaultHourCollect.class);
            String faulHourId = faultHourCollectService.insertFaultHourCollect(faultHourCollect, username);
            return ResultHelper.getResult(faulHourId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }

    /**
     * 修改设备异常数据
     * @param faultHourCollectDto
     * @param username
     */
    public ResultPojo updateFaultHourCollect(FaultHourCollectDto faultHourCollectDto, String username) {
        try {
            if (faultHourCollectDto == null || faultHourCollectDto.getFaulHourId() == null || 
            		faultHourCollectDto.getFaulHourId().equals(""))
            	return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            FaultHourCollect faultHourCollect = ReflectHelper.beanToBean(faultHourCollectDto, FaultHourCollect.class);
            String faulHourId = faultHourCollectService.updateFaultHourCollect(faultHourCollect, username);
            return ResultHelper.getResult(faulHourId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }

    }

    /**
     * 删除设备异常数据
     * @param faulHourId
     * @param username
     */
    public ResultPojo deleteFaultHourCollect(String faulHourId, String username) {
        try {
            if (faulHourId == null || faulHourId.equals(""))
                return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
            faultHourCollectService.deleteFaultHourCollect(faulHourId, username);
            return ResultHelper.getResult(faulHourId);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e, EnumAppCode.OPERATOR_FAILURE);
        }
    }
}
